package com.demo1;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MyClient1 {
    //发送图片
	public static void main(String[] args) throws Exception {
		//1.创建客户端对象
		Socket socket = new Socket("localhost",9001);
		//2.获取socket输出流
		OutputStream out = socket.getOutputStream();
		//3.读取发送的图片
		FileInputStream fis = new FileInputStream("e:\\a.jpg");
		//4.边读边写
		byte [] b = new byte[1024];
		int len=0;
		while(  (len=fis.read(b)) != -1 ){
			//5.向服务器发送数据
			out.write(b, 0, len);
		}
		//6.告诉服务器发送结束
		socket.shutdownOutput();
		
		//7.读取服务器返回的信息
		InputStream in = socket.getInputStream();
		//8.创建一个缓冲区
		byte [] buf = new byte[1024];
		//9.读取到字节数组--真实读取的长度
		len = in.read(buf);
		//10.输出服务器返回的信息
		System.out.println("服务器说:"+ new String(buf,0,len) );
		//11.关闭资源
		fis.close();
		in.close();
		socket.close();
	}

}
